package com.pds.web.testOrganizationInfo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pds.common.utils.DateUtils;
import com.pds.web.category.entity.PdsCategory;
import com.pds.web.category.entity.PdsCategoryStandard;
import com.pds.web.category.mapper.PdsCategoryMapper;
import com.pds.web.category.mapper.PdsCategoryStandardMapper;
import com.pds.web.testOrganizationInfo.domain.PdsTestOrganizationRange;
import com.pds.web.testOrganizationInfo.dto.CategoryRangeDTO;
import com.pds.web.testOrganizationInfo.dto.OrganizationRangeDTO;
import com.pds.web.testOrganizationInfo.mapper.PdsTestOrganizationRangeMapper;
import com.pds.web.testOrganizationInfo.service.PdsTestOrganizationRangeService;
import com.pds.web.testOrganizationInfo.vo.CategoryStandardVO;
import com.pds.web.testOrganizationInfo.vo.LargeCategoryVO;
import com.pds.web.testOrganizationInfo.vo.SmallCategoryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 检测机构标准范围表
 *
 * @author srf sunlightcs@gmail.com
 * @since 1.0.0 2024-03-11
 */
@Service("pdsTestOrganizationRangeService")
public class PdsTestOrganizationRangeServiceImpl extends ServiceImpl<PdsTestOrganizationRangeMapper, PdsTestOrganizationRange> implements PdsTestOrganizationRangeService {
    @Resource
    private PdsTestOrganizationRangeMapper pdsTestOrganizationRangeMapper;
    @Resource
    private PdsCategoryMapper pdsCategoryMapper;
    @Resource
    private PdsCategoryStandardMapper pdsCategoryStandardMapper;

    @Override
    public List<LargeCategoryVO> selectList(PdsTestOrganizationRange pdsTestOrganizationRange) {
        List<LargeCategoryVO> list = new ArrayList<>();
        Long organizationId = pdsTestOrganizationRange.getOrganizationId();
        if(!Objects.isNull(organizationId)){
            QueryWrapper<PdsTestOrganizationRange> largeWrapper = new QueryWrapper<>();
            largeWrapper.eq("organization_id",organizationId);
            largeWrapper.groupBy("large_category_id");
            List<PdsTestOrganizationRange> largeList = pdsTestOrganizationRangeMapper.selectList(largeWrapper);
            if(!Objects.isNull(largeList) && !largeList.isEmpty()){
                for (PdsTestOrganizationRange large : largeList) {
                    Long largeCategoryId = large.getLargeCategoryId();
                    PdsCategory largeCategory = pdsCategoryMapper.selectById(largeCategoryId);
                    LargeCategoryVO largeCategoryVO = new LargeCategoryVO();
                    BeanUtils.copyProperties(largeCategory,largeCategoryVO);
                    list.add(largeCategoryVO);
                }
            }
            if(!list.isEmpty()){
                for (LargeCategoryVO largeCategoryVO : list) {
                    Long largeCategoryId = largeCategoryVO.getId();
                    QueryWrapper<PdsTestOrganizationRange> smallWrapper = new QueryWrapper<>();
                    smallWrapper.eq("organization_id",organizationId);
                    smallWrapper.eq("large_category_id",largeCategoryId);
                    smallWrapper.groupBy("small_category_id");
                    List<PdsTestOrganizationRange> smallList = pdsTestOrganizationRangeMapper.selectList(smallWrapper);
                    if(!Objects.isNull(smallList) && !smallList.isEmpty()){
                        for (PdsTestOrganizationRange small : smallList) {
                            Long smallCategoryId = small.getSmallCategoryId();
                            PdsCategory smallCategory = pdsCategoryMapper.selectById(smallCategoryId);
                            SmallCategoryVO smallCategoryVO = new SmallCategoryVO();
                            BeanUtils.copyProperties(smallCategory,smallCategoryVO);
                            QueryWrapper<PdsTestOrganizationRange> standardWrapper = new QueryWrapper<>();
                            standardWrapper.eq("organization_id",organizationId);
                            standardWrapper.eq("large_category_id",largeCategoryId);
                            standardWrapper.eq("small_category_id",smallCategoryId);
                            List<PdsTestOrganizationRange> standardList = pdsTestOrganizationRangeMapper.selectList(standardWrapper);
                            if(!Objects.isNull(standardList) && !standardList.isEmpty()){
                                for (PdsTestOrganizationRange standard : standardList) {
                                    Long standardId = standard.getStandardId();
                                    PdsCategoryStandard categoryStandard = pdsCategoryStandardMapper.selectById(standardId);
                                    CategoryStandardVO categoryStandardVO = new CategoryStandardVO();
                                    BeanUtils.copyProperties(categoryStandard,categoryStandardVO);
                                    List<CategoryStandardVO> standardVOList = smallCategoryVO.getChildren();
                                    if(Objects.isNull(standardVOList)){
                                        standardVOList = new ArrayList<>();
                                    }
                                    standardVOList.add(categoryStandardVO);
                                    smallCategoryVO.setChildren(standardVOList);
                                }
                            }
                            List<SmallCategoryVO> smallVOList = largeCategoryVO.getChildren();
                            if(Objects.isNull(smallVOList)){
                                smallVOList = new ArrayList<>();
                            }
                            smallVOList.add(smallCategoryVO);
                            largeCategoryVO.setChildren(smallVOList);
                        }
                    }
                }
            }
        }
        return list;
    }

    @Override
    public PdsTestOrganizationRange queryById(Long id) {
        return pdsTestOrganizationRangeMapper.selectById(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public Integer insert(OrganizationRangeDTO organizationRangeDTO) {
        int total = 0;
        Date date = DateUtils.parseDate(DateUtils.getTime());
        Long organizationId = organizationRangeDTO.getOrganizationId();
        List<CategoryRangeDTO> categories = organizationRangeDTO.getCategories();
        if(!Objects.isNull(categories) && !categories.isEmpty()){
            for (CategoryRangeDTO category : categories) {
                PdsTestOrganizationRange range = new PdsTestOrganizationRange();
                BeanUtils.copyProperties(category,range);
                range.setOrganizationId(organizationId);
                range.setCreateTime(date);
                int count = pdsTestOrganizationRangeMapper.insert(range);
                total += count;
            }
        }
        return total;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public Integer update(OrganizationRangeDTO organizationRangeDTO) {
        int total = 0;
        Date date = DateUtils.parseDate(DateUtils.getTime());
        Long organizationId = organizationRangeDTO.getOrganizationId();
        QueryWrapper<PdsTestOrganizationRange> wrapper = new QueryWrapper<>();
        wrapper.eq("organization_id",organizationId);
        pdsTestOrganizationRangeMapper.delete(wrapper);
        List<CategoryRangeDTO> categories = organizationRangeDTO.getCategories();
        if(!Objects.isNull(categories) && !categories.isEmpty()){
            for (CategoryRangeDTO category : categories) {
                PdsTestOrganizationRange range = new PdsTestOrganizationRange();
                BeanUtils.copyProperties(category,range);
                range.setOrganizationId(organizationId);
                range.setCreateTime(date);
                int count = pdsTestOrganizationRangeMapper.insert(range);
                total += count;
            }
        }
        return total;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public Integer deleteByIds(List<Long> ids) {
        return pdsTestOrganizationRangeMapper.deleteBatchIds(ids);
    }
}